import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import { getlist ,deletelist , searchsou } from '@/api/search';

export interface IndexModelState {
  name: string;
}

import {message} from 'antd'
import Item from 'antd/lib/list/Item';

export interface IndexModelType {
  namespace: 'index';
  state: IndexModelState;
  effects: {
    query: Effect;
  };
  reducers: {
    save: Reducer<IndexModelState>;
    // 启用 immer 之后
    // save: ImmerReducer<IndexModelState>;
  };
  subscriptions: { setup: Subscription };
}

const IndexModel: any = {
  namespace: 'distchlist',

  state: {
    list:[],
    aa:[],
    list2:[],
  },

  effects: {
    *getlist({ payload }:any, { call, put }:any):any {
          const getlistdata:any = yield call (getlist)


          console.log(getlistdata);

            yield put({
                type:'setlist',
                payload:getlistdata.data.data
            })
    },
            //删除
    *deletelist({ payload }:any, { call, put }:any):any {
     const result = yield call (deletelist,payload)
     console.log(result);

        yield put({
            type:'setdel',
            payload:payload
        })

        if(result.status === 200){
          message.success('删除成功')
        }
},



    //搜索
    *searchsou({ payload }:any, { call, put }:any):any {
      const { in1,in2 } = payload;
      const result = yield call (searchsou,in1,in2)
      console.log(result.data);


         yield put({
             type:'sousuo',
             payload:{in1,in2}
         })
 },




  },


  reducers: {

    setlist(state:any, action:any) {
      console.log(action);
              state.list = action.payload
              state.list2 = action.payload
      return {
        ...state,
        ...action.payload,
      };
    },


        //删除
    setdel(state:any, action:any) {
      console.log(action);

       state.list.forEach((item:any,index:number) => {
                if(item.id === action.payload){
                  state.list.splice(index, 1)
                }
       });
      return {
        ...state,
        ...action.payload,
      };
    },



    //搜索
    sousuo(state:any, action:any) {
          state.aa = state.list[0].filter((item:any, index:number) => {
            return item.count.includes(action.payload.in2)
            console.log(item.count)
            console.log(action.payload.in2)
          })



   },

  },

}

export default IndexModel;
